package com.logos.cartoon.controller;

import com.logos.cartoon.result.Result;
import com.logos.cartoon.service.FileService;
import com.logos.cartoon.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileService fileService;

    @Resource
    private AliOssUtil aliOssUtil;

    @Value(value = "${storage.service.type}")
    private String storageType;

    @GetMapping("/testGetAllBuckets")
    public String testGetAllBuckets() throws Exception {
        List<String> allBucket = fileService.getAllBucket();
        return allBucket.toString();
    }


    @GetMapping("/testOss")
    public String testOss() throws Exception {
        return storageType;
    }

    @GetMapping("/getUrl")
    public String getUrl(String bucketName, String objectName) {
        log.info("getUrl:{},{}", bucketName, objectName);
        return fileService.getUrl(bucketName, objectName);
    }

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile uploadFile, String bucket, String objectName) throws Exception {
        log.info("uploadFile:{},bucket:{},objectName:{}", uploadFile, bucket, objectName);
        try {
            objectName = objectName + '/' + uploadFile.getOriginalFilename();
            String url = fileService.uploadFile(uploadFile, bucket, objectName);
            log.info("url:{}", url);
            return Result.ok(url);
        }catch (Exception e){
            log.error("FileController.upload.aiOss.fail：{}", e.getMessage(),e);
            return Result.fail("上传文件失败");
        }
    }

    /**
     * 阿里云文件上传
     *
     */
    @PostMapping("/ali/upload")
    public Result<String> upload(MultipartFile uploadFile) {
        log.info("开始文件上传：{}", uploadFile);
        try {
            // 获取原始文件名
            String originalFilename = uploadFile.getOriginalFilename();
            // 获取原始文件名的后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            // 得到新的文件名
            String freshFileName = UUID.randomUUID().toString() + extension;
            // 文件的请求路径
            String filePath = aliOssUtil.upload(uploadFile.getBytes(), freshFileName);
            return Result.ok(filePath);
        } catch (Exception e) {
            log.error("FileController.upload.aiOss.fail：{}", e.getMessage(),e);
            return Result.fail("上传文件失败");
        }
    }

}
